home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.09 Sep 96 / Getting Started / CWBlink.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.7 KB  |  266 lines  |  [TEXT/CWIE]

  1. package com.metrowerks.example.CWBlink;
  2.  
  3. import java.awt.*;
  4.  
  5. public class CWBlink extends java.applet.Applet
  6. {
  7.     private Button blinkButton;
  8.     private BlinkingText blinkText;
  9.  
  10.     public String getParameter(String name)
  11.     {
  12.         String result = null;
  13.         
  14.         try
  15.         {
  16.             result = super.getParameter(name);
  17.         }
  18.         catch ( Exception e )
  19.         {
  20.             result = null;
  21.         }
  22.         
  23.         return result;
  24.     }
  25.  
  26.     public void init()
  27.     {
  28.         Panel tempPanel;
  29.         
  30.         String att = getParameter("speed");
  31.         int speed = (att == null) ?
  32.             500 : (1000 / Integer.valueOf(att).intValue());
  33.         
  34.         setLayout( new BorderLayout() );
  35.  
  36.         att = getParameter("blinker");
  37.         String blinkString = (att == null) ?
  38.             "CodeWarrior!!!" : att;
  39.         
  40.         blinkButton = new Button( "Blink" );
  41.         
  42.         tempPanel = new Panel();
  43.         tempPanel.add( blinkButton );
  44.         this.add( "North", tempPanel );
  45.         
  46.         blinkText = new BlinkingText( blinkString, speed );
  47.         
  48.         tempPanel = new Panel();
  49.         tempPanel.add( blinkText );
  50.         this.add( "Center", tempPanel );
  51.         
  52.         resize( 570, 170 );
  53.     }
  54.     
  55.     public void start()
  56.     {
  57.         blinkText.start();
  58.     }
  59.     
  60.     public void stop()
  61.     {
  62.         blinkText.stop();
  63.     }
  64.     
  65.     public boolean action(Event evt, Object arg)
  66.     {
  67.         if ( "Blink".equals(arg) )
  68.         {
  69.             blinkText.ToggleBlinking();
  70.         }
  71.         return true;
  72.     }
  73.     
  74.     public static void main(String args[])
  75.     {
  76.         com.metrowerks.AppletFrame.startApplet(
  77.                 "com.metrowerks.example.CWBlink.CWBlink",
  78.                 "Blink", args);
  79.     }
  80. }
  81.  
  82.  
  83. class BlinkingText extends Canvas implements Runnable
  84. {
  85.     private Thread blinkThread = null;
  86.     private String blinkString;
  87.     private Font font;
  88.     private int speed;
  89.     private boolean isBlinking = false;
  90.     private Color[] letters;
  91.     private boolean[] is_black;
  92.     private int current_letter = 0;
  93.     private int old_width, old_height;
  94.     
  95.     private Color RandomColor()
  96.     {
  97.         int red, green, blue;
  98.         
  99.         do {
  100.             red = (int)(Math.random() * 256);
  101.         } while ( red > 0x8000 );
  102.         
  103.         do {
  104.             green = (int)(Math.random() * 256);
  105.         } while ( green > 0x8000 );
  106.         
  107.         do {
  108.             blue = (int)(Math.random() * 256);
  109.         } while ( blue > 0x8000 );
  110.         
  111.         return new java.awt.Color( red, green, blue );
  112.     }
  113.     
  114.     public BlinkingText( String blinkString, int speed )
  115.     {
  116.         this.blinkString = blinkString;
  117.         this.speed = speed / blinkString.length();
  118.         this.font = new java.awt.Font( "TimesRoman",
  119.                                                                 Font.PLAIN, 64 );
  120.         this.letters = new Color[blinkString.length()];
  121.         this.is_black = new boolean[blinkString.length()];
  122.         
  123.         for ( int i = 0; i < letters.length; i++ )
  124.         {
  125.             letters[i] = RandomColor();
  126.             is_black[i] = true;
  127.         }
  128.         resize( 530, 70 );
  129.         repaint();
  130.     }
  131.     
  132.     public synchronized void ToggleBlinking()
  133.     {
  134.         isBlinking = ! isBlinking;
  135.         notify();
  136.     }
  137.     
  138.     public synchronized void PaintLetter( int the_letter )
  139.     {
  140.         Rectangle b = bounds();
  141.         int width = b.width;
  142.         int height = b.height;
  143.         Graphics g = getGraphics();
  144.         
  145.         if ( old_width == width && old_height == height )
  146.         {
  147.             int x = 0;
  148.             int y = height;
  149.  
  150.             g.setFont(font);
  151.             FontMetrics fm = g.getFontMetrics();
  152.             
  153.             for (int index=0; index<the_letter; index++ )
  154.             {
  155.                 int w = fm.charWidth(blinkString.charAt(index));
  156.                 x += w;
  157.             }
  158.             
  159.             int w = fm.charWidth(blinkString.charAt(the_letter));
  160.             g.setColor( isBlinking ? 
  161.                                 letters[the_letter] : Color.black );
  162.             g.clearRect( x, 0, w, height );
  163.             g.drawString( blinkString.substring( 
  164.                                 the_letter,the_letter+1), x, y );
  165.  
  166.             is_black[the_letter] = !isBlinking;
  167.         }
  168.         else
  169.         {
  170.             paint( g );
  171.         }
  172.     }
  173.     
  174.     public synchronized void paint(Graphics g)
  175.     {
  176.         Rectangle b = bounds();
  177.         int width = b.width;
  178.         int height = b.height;
  179.         old_width = width;
  180.         old_height = height;
  181.         int x = 0;
  182.         int y = height;
  183.  
  184.         g.setColor(Color.black);
  185.         g.setFont(font);
  186.         FontMetrics fm = g.getFontMetrics();
  187.         
  188.         g.clearRect( 0, 0, width, height );
  189.         if ( isBlinking )
  190.         {
  191.             for (int index=0; index<blinkString.length();
  192.                         index++ )
  193.             {
  194.                 int w = fm.charWidth(blinkString.charAt(index));
  195.                 
  196.                 g.setColor( letters[index] );
  197.                 
  198.                 g.drawString(
  199.                     blinkString.substring(index,index+1), x, y);
  200.                 x += w;
  201.             }
  202.         }
  203.         else
  204.         {
  205.             g.drawString(blinkString, x, y );
  206.         }
  207.         
  208.         for ( int index=0; index<blinkString.length(); index++ )
  209.         {
  210.             is_black[index] = !isBlinking;
  211.         }
  212.     }
  213.  
  214.     public void start()
  215.     {
  216.         stop();
  217.         blinkThread = new Thread(this);
  218.         blinkThread.start();
  219.     }
  220.     
  221.     public void stop()
  222.     {
  223.         if ( blinkThread != null )
  224.         {
  225.             blinkThread.stop();
  226.             blinkThread = null;
  227.         }
  228.     }
  229.     
  230.     public void run()
  231.     {
  232.         while (true)
  233.         {
  234.             synchronized ( this )
  235.             {
  236.                 try
  237.                 {
  238.                     wait( 1 );
  239.                 }
  240.                 catch (Exception e)
  241.                 {
  242.                 }
  243.                 
  244.                 if ( isBlinking || !is_black[current_letter] )
  245.                 {
  246.                     if ( isBlinking )
  247.                     {
  248.                         letters[current_letter] = RandomColor();
  249.                     }
  250.                     PaintLetter( current_letter );
  251.                 }
  252.                 current_letter++;
  253.                 if ( current_letter >= blinkString.length() )
  254.                 {
  255.                     current_letter = 0;
  256.                 }
  257.             }
  258.          }
  259.     }
  260.  
  261.     public void finalize()
  262.     {
  263.         stop();
  264.     }
  265. }
  266.